package com.AEWMS.Service;

import com.AEWMS.Dao.Admin;
import com.AEWMS.Dao.Class;
import com.AEWMS.Utlis.DbCon;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 * @author lhs
 * @create 2021/2/22 16:16
 */
public class ClassService {

    private Connection conn = null ;
    private PreparedStatement pstmt = null ;

    public ClassService() {
        DbCon dbcon;
        try {
            dbcon = new DbCon();
            this.conn = dbcon.getConnection();
        } catch (Exception e) {
            System.out.println("StudentService 连接数据库失败");
            e.printStackTrace();
        }
    }

    public void closeCon(){
        try {
            this.conn.close();
        } catch (Exception e) {
            System.out.println("数据库连接关闭");
            e.printStackTrace();
        }
    }

    /**
     * 查找所有班级
     * @return 所有班级信息
     */
    public ArrayList<Class> findAllClass(){
        ArrayList<Class> allClassList = new ArrayList();//升级版数组，返回所有班级对象
        String sql = "SELECT * FROM class_info";
        try {
            this.pstmt = conn.prepareStatement(sql);
            ResultSet rs = this.pstmt.executeQuery();
            while (rs.next()){
                Class s = new Class();//按行查找，为false时查询结束
                s.setClassId(rs.getInt(1));
                s.setCollegeId(rs.getString(2));
                s.setGradeId(rs.getString(3));
                s.setMajorId(rs.getString(4));
                s.setClassName(rs.getString(5));

                allClassList.add(s);//将查找到的行（每个班级对象）添加到数组
            }
        } catch (Exception e) {
            System.out.println("findAllClass 失败");
            System.out.println(e);
        }
        return allClassList;//输出包含所有学生对象的数组
    }

    /**
     * 查找班级
     * @param id 通过id
     * @return 返回一个班级对象
     */
    public Class findClassById(String id){
        Class s = new Class();
        String sql = "SELECT * FROM class_info WHERE class_id = ?";
        try {
            this.pstmt = conn.prepareStatement(sql);//将sql语句放到数据库的准备对象
            this.pstmt.setString(1,id);//将传入id值替换问号放在SQL
            ResultSet rs = this.pstmt.executeQuery();//执行查询返回结果集
            while (rs.next()){
                s.setClassId(rs.getInt(1));
                s.setCollegeId(rs.getString(2));
                s.setGradeId(rs.getString(3));
                s.setMajorId(rs.getString(4));
                s.setClassName(rs.getString(5));
            }
        }
        catch (Exception e) {
            System.out.println("findStuById 失败");
            System.out.println(e);
        }
        return s;
    }

    /**
     * 添加班级
     * @return 一个班级对象
     */
    public boolean insertClass(Class s){
        boolean flag = false;
        String sql = "INSERT INTO class_info VALUES(?,?,?,?,?)";
        try {
            this.pstmt = conn.prepareStatement(sql);//sql语句已经在这个准备对象里，有问号就要用准备对象
            this.pstmt.setInt(1,s.getClassId());
            this.pstmt.setString(2,s.getCollegeId());
            this.pstmt.setString(3,s.getGradeId());
            this.pstmt.setString(4,s.getMajorId());
            this.pstmt.setString(5,s.getClassName());//要和数据库顺序一致


            int rs = this.pstmt.executeUpdate();//这里不用再传sql
            if ( rs == 1 ) {
                flag = true;
            }
        }
        catch (Exception e){
            System.out.println("insertClass defeat");
            System.out.println(e);
        }
        return flag;
    }

    /**
     * 修改一个班级
     * @param s 一个班级对象
     * @return 是否成功
     */
    public boolean updateClass(Class s) {
        boolean flag = false;
        String sql = "UPDATE class_info SET class_name = ?,college_id = ?,grade_id = ?,major_name = ?  WHERE class_id = ?";//这里的字段名要与数据库的一致
        try {
            this.pstmt = conn.prepareStatement(sql);
            this.pstmt.setString(1,s.getClassName());
            this.pstmt.setString(2,s.getCollegeId());
            this.pstmt.setString(3,s.getGradeId());
            this.pstmt.setString(4,s.getMajorId());
            this.pstmt.setInt(5,s.getClassId());
            int rs = this.pstmt.executeUpdate();
            if ( rs == 1 ) {
                flag = true;
            }
        } catch (Exception e) {
            System.out.println("updateAStu 失败");
            System.out.println(e);
        }
        return flag;

    }

    /**
     * 删除一个班级
     * @param id 班级id
     * @return 是否成功
     */
    public boolean deleteClassById(String id){
        boolean flag = false;
        String sql = "DELETE FROM class_info WHERE class_id = ?";//这里的字段名要与数据库的一致
        try {
            this.pstmt = conn.prepareStatement(sql);
            this.pstmt.setString(1,id);//这里的名字与dao.admin传进来的一致
            int rs = this.pstmt.executeUpdate();
            if ( rs == 1 ) {
                flag = true;
            }
        }
        catch (Exception e){
            System.out.println("deleteClassById 失败");
            System.out.println(e);
        }
        return flag;
    }

}


